package giee.com.sqltransfer.utils;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * 工具类，用来解决映射表命名的问题
 * 命名规则：模拟机编号+sql文件名
 * sql文件名和导入表类型保持一致
 */
public class TableNameRules {

    /**
     * 模拟从数据库中获取模拟机映射类型
     * @return 返回值为Map<String,String> 类型
     */
    public static Map<String,String> getPreMappingData(){
        Map<String,String> map = new HashMap<>();
        map.put("秦山CANDU","QS_CANDU_SIM");
        map.put("田湾四号模拟机","TW_78_SIM");
        map.put("田湾三号模拟机","TW_34_SIM");
        map.put("海南一号模拟机","HN_12_SIM");
        map.put("三门一号模拟机","SM_01_SIM");
        map.put("海南小堆模拟机","HN_01_SIM");
        map.put("山东一号模拟机","SD_12_SIM");
        map.put("霞浦一号模拟机","XP_12_SIM");
        return map;
    }

    /**
     * 获取导入表类型
     * @return  返回值为Map<String,String> 类型
     */
    public static Map<String,String> getTableType() {
        Map<String,String> map = new HashMap<>();
        map.put("故障","mftable");
        map.put("就地","rftable");
        map.put("超控","ovrtable");
        map.put("快时因子","speedupvar");
        map.put("外部参数","externvars");
        map.put("重要参数","icvars");
        map.put("曲线参数","curvevars");
        map.put("监视参数","watchvars");
        return map;
    }

    /**
     * 从数据库中获取模拟机映射类型
     * @return 返回值为Map<String,String> 类型
     */
    public static Map<String,String> getSimulatorMappingRules() {
        final String URl = "jdbc:mysql://10.30.149.123:10110/masterportal";
        final String USER = "root";
        final String PASSWORD = "ML3@gH8MqN";

        Connection conn_a = null;
        Statement stmt_a = null;
        ResultSet rs_a = null;
        Map<String,String> map_a = new HashMap<>();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn_a = DriverManager.getConnection(URl, USER, PASSWORD);
            stmt_a = conn_a.createStatement();

            String sql = "SELECT * FROM simulator_info";
            rs_a = stmt_a.executeQuery(sql);

            while (rs_a.next()) {
                map_a.put(rs_a.getString("sim_name"), rs_a.getString("sim_code"));
            }
            stmt_a.close();
            conn_a.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,
                    "无法获取命名映射规则，请检查数据库连接！", "错误", JOptionPane.ERROR_MESSAGE);
        }
        return map_a;
    }
}
